﻿@using Microsoft.AspNetCore.Mvc.Localization
@model OCPP.Core.Management.Models.ChargeReportViewModel
@inject IViewLocalizer Localizer

<style>
    .compact-table th, .compact-table td {
        padding: 2px 5px !important;
        margin: 0 !important;
    }
</style>

@{
    ViewData["Title"] = Localizer["Title"];
    DateTime dtNow = DateTime.Now;
}

<h1>@ViewData["Title"]</h1>
<br />

<form method="get" asp-action="ChargeReport" class="form-inline">
    <div class="container">
        <div class="row">
            <div class="btn-group mb-2 ml-2">
                <a href="@Url.Action("ChargeReport", new { startDate = dtNow.ToString("yyyy-01-01"), stopDate = dtNow.ToString("yyyy-MM-dd") })" class="btn btn-secondary">@Localizer["YearToDate"]</a>
                <a href="@Url.Action("ChargeReport", new { startDate = dtNow.AddYears(-1).ToString("yyyy-01-01"), stopDate = dtNow.AddYears(-1).ToString("yyyy-12-31") })" class="btn btn-secondary">@Localizer["PreviousYear"]</a>
                <a href="@Url.Action("ChargeReport", new { startDate = dtNow.AddMonths(-1).ToString("yyyy-MM-01"), stopDate = new DateTime(dtNow.Year, dtNow.Month, 1).AddDays(-1).ToString("yyyy-MM-dd") })" class="btn btn-secondary">@Localizer["PreviousMonth"]</a>
                <a href="@Url.Action("ChargeReport", new { startDate = dtNow.AddDays(-(int)dtNow.DayOfWeek - 7).ToString("yyyy-MM-dd"), stopDate = dtNow.AddDays(-(int)dtNow.DayOfWeek).AddDays(-1).ToString("yyyy-MM-dd") })" class="btn btn-secondary">@Localizer["PreviousWeek"]</a>
            </div>
        </div>

        <div class="row">
            <div class="form-group mb-2 ml-2">
                <label for="startDate" class="sr-only">@Localizer["StartDate"]</label>
                <input type="date" id="startDate" name="startDate" class="form-control" value="@Model.StartDate.ToString("yyyy-MM-dd")" />
            </div>
            <div class="form-group mx-sm-3 mb-2">
                <label for="stopDate" class="sr-only">@Localizer["StopDate"]</label>
                <input type="date" id="stopDate" name="stopDate" class="form-control" value="@Model.StopDate.ToString("yyyy-MM-dd")" />
            </div>
            <button type="submit" class="btn btn-primary mb-2">@Localizer["Submit"]</button>
        </div>
    </div>
</form>
<br />
@if (Model.Groups.Count == 0)
    @Localizer["NoData"]
else
{
    <p>&nbsp;</p>
    <div class="container">
        <div class="row">
            <div class="col-sm-10">
                <b>@String.Format(Localizer["ReportTitle"].Value, @Model.StartDate.ToShortDateString(), @Model.StopDate.ToShortDateString())</b>
            </div>
            <div class="col-sm-1 align-self-end">
                @Localizer["ExpSummery"]&nbsp;
            </div>
            <div class="col-sm-1 align-self-end">
                <a href="@Url.Action("ChargeReportCsv", new { startDate = Model.StartDate.ToString("yyyy-MM-dd"), stopDate = Model.StopDate.ToString("yyyy-MM-dd") })" title="@Localizer["ExpSummaryCSVTooltipp"]">
                    <i class="fas fa-file-csv"></i>
                </a>
                <a href="@Url.Action("ChargeReportXlsx", new { startDate = Model.StartDate.ToString("yyyy-MM-dd"), stopDate = Model.StopDate.ToString("yyyy-MM-dd") })" title="@Localizer["ExpSummaryXLSXTooltipp"]">
                    <i class="fas fa-file-excel"></i>
                </a>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-10">
                &nbsp;
            </div>
            <div class="col-sm-1 align-self-end">
                @Localizer["ExpDetails"]&nbsp;
            </div>
            <div class="col-sm-1 align-self-end">
                <a href="@Url.Action("AllTransactionsCsv", new { startDate = Model.StartDate.ToString("yyyy-MM-dd"), stopDate = Model.StopDate.ToString("yyyy-MM-dd") })" title="@Localizer["ExpDetailsCSVTooltipp"]">
                    <i class="fas fa-file-csv"></i>
                </a>
                <a href="@Url.Action("AllTransactionsXlsx", new { startDate = Model.StartDate.ToString("yyyy-MM-dd"), stopDate = Model.StopDate.ToString("yyyy-MM-dd") })" title="@Localizer["ExpDetailsXLSXTooltipp"]">
                    <i class="fas fa-file-excel"></i>
                </a>
            </div>
        </div>
    </div>

    @foreach (var group in Model.Groups)
    {
        <br/>
        <h3>@group.GroupName</h3>
        <table id="dtChargeTags" class="table table-striped table-bordered table-sm table-hover" cellspacing="0" width="100%">
            <thead>
                <tr>
                    <th class="th-sm">@Localizer["TagName"]</th>
                    <th class="th-sm">@Localizer["Energy-kWh"]</th>
                </tr>
            </thead>
            <tbody>
                @foreach (var tag in group.Tags)
                {
                    var totalEnergy = tag.Transactions
                    .Where(t => t.Energy.HasValue)
                    .Sum(t => t.Energy.Value);

                    <tr>
                        <td>@tag.TagName</td>
                        <td>@(Math.Round(totalEnergy, 2)) kWh</td>
                    </tr>
                }
            </tbody>
        </table>
    }
}
